<?php

/*
*
*   Excalibur Content Management System
*   Copyright © 2008 Egor "Sontan" Kuryanovich
*
*   Based on Explay Engine v2.0 by Golovdinov Alexander
*
*   Official site: www.excms.ru
*   Contact e-mail: support@excms.ru
*
*   GNU General Public License original source:
*   http://www.gnu.org/licenses/gpl-3.0.html
*
*/

/************************************************/
//
//	ЯДРО СИСТЕМЫ: ВЫВОД СТРАНИЦЫ
//
/************************************************/


class CORE {
	
	private $db;
	private $template;
	public $module_name = 'start_page';
	public $global_template_href;
	public $global_template_html;
	
	private $user_group;
	
	public $body;
	public $title;
	
	public $SITE = array ();
	
	public $second_navigation;
	public $panels_top;
	public $panels_bottom;
	public $blocks_left;
	public $blocks_right;
	
	
	public function __construct ($db, $template, $SITE, $global_template_href, $user_group, $module_name = "") {
		$this->db = $db;
		$this->template = $template;
		$this->SITE = $SITE;
		$this->module_name = $module_name;
		$this->user_group = $user_group;
		$this->global_template_href = $global_template_href;
	}
	
	/* ********************************************** */
	// СЕССИЯ ПОЛЬЗОВАТЕЛЯ
	/* ********************************************** */
	
	public function session ($group, $uid, $sessid, $page_name) {
		
		//$page_name = $this->title;
		$ip = $_SERVER['REMOTE_ADDR'];
		
		$get_usr = $this->db->query ("SELECT * FROM ".DB_PEREFIX."_sessions WHERE sesid = '$sessid'");
		if ($this->db->num_rows($get_usr) == 0) $this->db->query ("INSERT INTO ".DB_PEREFIX."_sessions VALUES ('$group', '".time()."', '$uid', '$ip', '$page_name', '$sessid')");
		else $this->db->query ("UPDATE ".DB_PEREFIX."_sessions SET utime = '".time()."', uid = '$uid', ugroup = '$group', page = '$page_name' WHERE sesid = '$sessid'");
		$this->db->query ("DELETE FROM ".DB_PEREFIX."_sessions WHERE utime <".(time()-300));
	}
	
	/* ********************************************** */
	// СОЗДАЕМ СТРАНИЦУ ИЗ ШАБЛОНА
	/* ********************************************** */
	
	public function make_page () {
		
		($this->title == "") ? $title = $this->SITE['site_name'] : $title = $this->title." &mdash; ".$this->SITE['site_name'];
		
		$this->template->set_var ('{TITLE}', $title);
		$this->template->set_var ('{DESCRIPTION}', $this->SITE['site_description']);
		$this->template->set_var ('{KEYWORDS}', $this->SITE['site_keywords']);
		$this->global_template_html = $this->template->display ($this->global_template_href);
		
		preg_match_all ('#<include_php=(.+?)>#is', $this->global_template_html, $include_php);
		for ($i=0; $i<count($include_php[0]); $i++) $this->include_php ("<include_php={$include_php[1][$i]}>");
		
		
		if (preg_match('#{left_blocks}#is', $this->global_template_html) || preg_match('#{right_blocks}#is', $this->global_template_html)) $this->make_blocks ();
		if (preg_match('#{top_panels}#is', $this->global_template_html) || preg_match('#{bottom_panels}#is', $this->global_template_html)) $this->make_panels ();
		
		if (preg_match('/\bleft_blocks\b/i', $this->global_template_html) || preg_match('/\right_blocks\b/i', $this->global_template_html)) $this->make_blocks ();
		if (preg_match('/\top_panels\b/i', $this->global_template_html) || preg_match('/\bottom_panels\b/i', $this->global_template_html)) $this->make_panels ();
		
		$this->prepare_template ();
		
		print $this->global_template_html;
	}
	
	// подготовка к выводу
	public function prepare_template () {
		$this->global_template_html = str_replace ('{LEFT_BLOCKS}', $this->blocks_left, $this->global_template_html);
		$this->global_template_html = str_replace ('{RIGHT_BLOCKS}', $this->blocks_right, $this->global_template_html);
		$this->global_template_html = str_replace ('{TOP_PANELS}', $this->panels_top, $this->global_template_html);
		$this->global_template_html = str_replace ('{BOTTOM_PANELS}', $this->panels_bottom, $this->global_template_html);
		$this->global_template_html = str_replace ('{SECOND_NAVIGATION}', $this->second_navigation, $this->global_template_html);
		$this->global_template_html = str_replace ('{BODY}', $this->body, $this->global_template_html);
	}
	
	/* ********************************************** */
	// ВЫБОРКА БЛОКОВ
	/* ********************************************** */
	
	private function make_blocks () {
		($this->module_name == '') 
			? $page = 'start_page' 
			: $page = $this->module_name;
		
		$get_blocks = $this->db->query ("SELECT block_header, block_name, block_body, block_file, block_group, block_align, block_pages FROM ".DB_PEREFIX."_blocks WHERE block_visible = 'visible' ORDER BY block_pos");
		
		while (list($header, $name, $body, $file, $group, $align, $pages) = $this->db->fetch_row ($get_blocks)) {
			$module_allowed = false;
			$group_allowed = false;
			
			if ($pages != 'all') {
				$pages = explode (',', $pages);
				foreach ($pages as $p) if ($p == $page) $module_allowed = true;
			}
			else $module_allowed = true;
			
			if ($group == 'all' || is_registered()) $group_allowed = true;
			
			// input block
			
			if ($module_allowed && $group_allowed) ($file != '' && $body == '') ? block ($header, $name, $file, 'f', $align) : block ($header, $name, $body, 't', $align);
		}
	}
	
	
	/* ********************************************** */
	// ПАНЕЛИ
	/* ********************************************** */
	
	private function make_panels () {
		
		($this->module_name == '' || $this->module_name == 'start_page') 
			? $module_name = 'start' 
			: $module_name = $this->module_name;
		
		$get_panels = $this->db->query ("SELECT * FROM ".DB_PEREFIX."_panels WHERE panel_status = 'on' AND (panel_pages = 'all' OR panel_pages = '$module_name')");

		while ($panel = $this->db->fetch_array ($get_panels)) {
			if ($panel['panel_day'] != 0 && $panel['panel_day'] != "") {
				$day = $panel['panel_day'] * 86400;
				$ost = ($panel['panel_date'] + $day) - time();
				if ($ost < 0) $db->query ("UPDATE ".DB_PEREFIX."_panels SET panel_status = 'off' WHERE panel_id = '".$panel['panel_id']."'");
			}
			if ($panel['panel_group'] == "all" || is_registered()) {
				if ($panel['panel_align'] == "top") {
					if ($panel['panel_file'] != "") CORE_include_panel ($panel['panel_file'], $panel['panel_align']);
					elseif ($panel['panel_file'] == "" && $panel['panel_table'] == 'use') $this->panels_top .= past_table ($panel['panel_header'], $panel['panel_body'], true);
					else $this->panels_top .= $panel['panel_body'];
				} else {
					if ($panel['panel_file'] != "") CORE_include_panel ($panel['panel_file'], $panel['panel_align']);
					elseif ($panel['panel_file'] == "" && $panel['panel_table'] == 'use') $this->panels_bottom .= past_table ($panel['panel_header'], $panel['panel_body'], true);
					else $this->panels_bottom .= $panel['panel_body'];
				}
			}
		}
	}
	
	
	
	/* ********************************************** */
	// ПОДКЛЮЧЕНИЕ В ШАБЛОН PHP-ФАЙЛА
	/* ********************************************** */
	
	// $var - переменная в шаблоне вида <include_php=file.php> из которой достается имя файла
	// $replace_var - переменная, которая заменятся в шаблоне (копия $var)
	
	private function include_php ($var) 
	{
		$replace_var = $var;
		$var = str_replace ('<include_php=', '', $var);
		$var = str_replace ('>', '', $var);
		$var = str_replace ('"', '', $var);
		$var = str_replace ("'", '', $var);
		
		(file_exists ($_SERVER['DOCUMENT_ROOT'].'/'.$var)) 
			? CORE_include_php ($var, $replace_var )
			: $this->global_template_html = str_replace ($replace_var, 'Requested file not found!', $this->global_template_html);
	}
	
}


// Функция для подключения php-файлов в шаблон ядра

function CORE_include_php ($file, $replace) 
{
	global $db, $template, $GLOBAL_USER, $SITE, $CORE;
	include $_SERVER['DOCUMENT_ROOT'].'/'.$file;
}

function CORE_include_panel ($file, $align) 
{
	global $db, $template, $GLOBAL_USER, $SITE, $CORE;
	include $_SERVER['DOCUMENT_ROOT'].'/'.$file;
}	
